/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/commons/json/serializer/DateSerializer.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: DateSerializer.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.json.serializer;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import com.elancom.framework.commons.json.util.IOUtils;

/**
 * @author elancom
 */
public class DateSerializer implements ObjectSerializer {

	public final static DateSerializer instance = new DateSerializer();

	@Override
	public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
		final SerializeWriter out = serializer.getWriter();

		if (object == null) {
			out.writeNull();
			return;
		}

		if (out.isEnabled(SerializerFeature.WriteClassName)) {
			if (object.getClass() != fieldType) {
				if (object.getClass() == java.util.Date.class) {
					out.write("new Date(");
					out.writeLongAndChar(((Date) object).getTime(), ')');
				} else {
					out.write('{');
					out.writeFieldName("@type");
					serializer.write(object.getClass().getName());
					out.writeFieldValue(',', "val", ((Date) object).getTime());
					out.write('}');
				}
				return;
			}
		}

		final Date date = (Date) object;

		if (out.isEnabled(SerializerFeature.WriteDateUseDateFormat)) {
			final DateFormat format = serializer.getDateFormat();
			final String text = format.format(date);
			out.writeString(text);
			return;
		}

		final long time = date.getTime();
		if (serializer.isEnabled(SerializerFeature.UseISO8601DateFormat)) {
			if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
				out.append('\'');
			} else {
				out.append('\"');
			}

			final Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(time);

			final int year = calendar.get(Calendar.YEAR);
			final int month = calendar.get(Calendar.MONTH) + 1;
			final int day = calendar.get(Calendar.DAY_OF_MONTH);
			final int hour = calendar.get(Calendar.HOUR_OF_DAY);
			final int minute = calendar.get(Calendar.MINUTE);
			final int second = calendar.get(Calendar.SECOND);
			final int millis = calendar.get(Calendar.MILLISECOND);

			char[] buf;
			if (millis != 0) {
				buf = "0000-00-00T00:00:00.000".toCharArray();
				IOUtils.getChars(millis, 23, buf);
				IOUtils.getChars(second, 19, buf);
				IOUtils.getChars(minute, 16, buf);
				IOUtils.getChars(hour, 13, buf);
				IOUtils.getChars(day, 10, buf);
				IOUtils.getChars(month, 7, buf);
				IOUtils.getChars(year, 4, buf);

			} else {
				if (second == 0 && minute == 0 && hour == 0) {
					buf = "0000-00-00".toCharArray();
					IOUtils.getChars(day, 10, buf);
					IOUtils.getChars(month, 7, buf);
					IOUtils.getChars(year, 4, buf);
				} else {
					buf = "0000-00-00T00:00:00".toCharArray();
					IOUtils.getChars(second, 19, buf);
					IOUtils.getChars(minute, 16, buf);
					IOUtils.getChars(hour, 13, buf);
					IOUtils.getChars(day, 10, buf);
					IOUtils.getChars(month, 7, buf);
					IOUtils.getChars(year, 4, buf);
				}
			}

			out.write(buf);

			if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
				out.append('\'');
			} else {
				out.append('\"');
			}
		} else {
			out.writeLong(time);
		}
	}
}
